home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 23 / Amiga Format AFCD23 (Feb 1998, Issue 107).iso / -seriously_amiga- / shareware / programming / other / fdi / fdi.c < prev    next >
C/C++ Source or Header  |  1997-12-01  |  6KB  |  275 lines

  1. /* Include files */
  2. #include <exec/types.h>
  3. #include <dos/dos.h>
  4. #include <dos/rdargs.h>
  5. #include <clib/exec_protos.h>
  6. #include <clib/dos_protos.h>
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12.  
  13. /* Fuctions */
  14. void OutputBanner(FILE *out_file);
  15. void OutputUsage(void);
  16.  
  17. /* Constants and shortcuts */
  18. #define PROG_NAME    "FDI"
  19. #define PROG_VERS    "2.2"
  20. #define PROG_DATE    __DATE__
  21.  
  22. #define CTRL_C       (SetSignal(NULL,NULL) & SIGBREAKF_CTRL_C)
  23. #define BREAK_TXT    "***Break - "PROG_NAME
  24.  
  25. #define MAXSTRING    256
  26.  
  27. #define MAXTABSDEF   4
  28.  
  29. /* Structures and related constants */
  30.  
  31. #define FA_TEMPLATE  "FROM/A,TO,MAXTABS/N,CALL/S,RCALL/S,SCALL/S,DEC/S,PRIV/S,NOBAN/S,QUIET/S"
  32.  
  33. struct FDIArgs {
  34.   STRPTR  FD_NAME;
  35.   STRPTR  INC_NAME;
  36.   LONG  *MAXTABS;
  37.   LONG  CALL;
  38.   LONG  RCALL;
  39.   LONG  SCALL;
  40.   LONG  DEC;
  41.   LONG  PRIV;
  42.   LONG  NOBAN;
  43.   LONG  QUIET;
  44. };
  45.  
  46.  
  47. /* Information for C:Version */
  48. const char verstr[] = "$VER: "PROG_NAME" "PROG_VERS" ("PROG_DATE")";
  49.  
  50.  
  51. void main()
  52. {
  53.   struct RDArgs *rdargs = NULL;
  54.   struct FDIArgs FDIArgs = {NULL,NULL,NULL,
  55.                             FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
  56.   
  57.   FILE *fd_file;
  58.   FILE *inc_file;
  59.   FILE *nil_file;
  60.   FILE *out_file;
  61.   
  62.   char input_string[MAXSTRING];
  63.   char base_string[MAXSTRING];
  64.   char macro_string[MAXSTRING];
  65.   
  66.   BOOL end_def=FALSE;
  67.   BOOL base_def=FALSE;
  68.   BOOL private=FALSE;
  69.   
  70.   int lvo_bias=0;
  71.   int maxtabs;
  72.   int tabs;
  73.   
  74.   int i;
  75.   
  76.   
  77.   /* Collect and analyse arguments */
  78.   rdargs = ReadArgs(FA_TEMPLATE,(ULONG *)&FDIArgs,NULL);
  79.   if (rdargs == NULL)
  80.   {
  81.     OutputUsage();
  82.     exit(0);
  83.   }
  84.   
  85.   nil_file = fopen("NIL:","w");
  86.  
  87.   /* Set output */
  88.   if (FDIArgs.QUIET)
  89.     out_file = nil_file;
  90.   else
  91.     out_file = stderr;
  92.  
  93.  
  94.   if (!FDIArgs.NOBAN)
  95.     OutputBanner(out_file);
  96.  
  97.  
  98.   /* Open FD file for input */
  99.   fd_file = fopen(FDIArgs.FD_NAME,"r");
  100.   if (!fd_file)
  101.   {
  102.     fprintf(out_file,"Couldn't open %s for input\n",FDIArgs.FD_NAME);
  103.     fclose(nil_file);
  104.     FreeArgs(rdargs);
  105.     exit(1);
  106.   }
  107.   
  108.   /* Open Include file for output (if specified) */
  109.   if (FDIArgs.INC_NAME)
  110.   {
  111.     inc_file = fopen(FDIArgs.INC_NAME,"w");
  112.     if (!inc_file)
  113.     {
  114.       fprintf(out_file,"Couldn't open %s for output\n",FDIArgs.INC_NAME);
  115.       fclose(nil_file);
  116.       fclose(fd_file);
  117.       FreeArgs(rdargs);
  118.       exit(1);
  119.     }
  120.   }
  121.   else
  122.     inc_file = stdout;
  123.   
  124.   
  125.   /* Set maximum number of tabs */
  126.   if (FDIArgs.MAXTABS)
  127.     maxtabs = *FDIArgs.MAXTABS;
  128.   else
  129.     maxtabs = MAXTABSDEF;
  130.   
  131.   
  132.   /*--- Processing ---*/
  133.   
  134.   fprintf(out_file,"Processing %s\n",FDIArgs.FD_NAME);
  135.   
  136.   /* Write header comment to include file */
  137.   fprintf(inc_file,"** Converted from \"%s\" by %s v%s\n",FDIArgs.FD_NAME,
  138.                  PROG_NAME,
  139.                  PROG_VERS);
  140.   fprintf(inc_file,"** %s © %s Karl J. Ots\n\n",PROG_NAME,PROG_DATE);
  141.   
  142.   /* Process FD File */
  143.   while(!feof(fd_file) && !end_def)
  144.   {
  145.     if (CTRL_C)
  146.     {
  147.       fputs(BREAK_TXT"\n",out_file);
  148.       fclose(nil_file);
  149.       fclose(fd_file);
  150.       if (FDIArgs.INC_NAME)
  151.         fclose(inc_file);
  152.       FreeArgs(rdargs);
  153.       exit(0);
  154.     }
  155.   
  156.     fgets(input_string,MAXSTRING,fd_file);
  157.  
  158.     if (input_string[0] == '*')
  159.       fputs(input_string,inc_file);
  160.     
  161.     else if (!strncmp("##base ",input_string,7))
  162.     {
  163.       strcpy(base_string,input_string+7);
  164.       base_def = TRUE;
  165.     }
  166.     else if (!strncmp("##bias ",input_string,7))
  167.       sscanf(input_string+7,"%d",&lvo_bias);
  168.     
  169.     else if (!strncmp("##public",input_string,8))
  170.       private = FALSE;
  171.     
  172.     else if (!strncmp("##private",input_string,9))
  173.       private = TRUE;
  174.     
  175.     else if (!strncmp("##end",input_string,5))
  176.       end_def = TRUE;
  177.     
  178.     else if (strncmp("##",input_string,2))
  179.     {
  180.       if (!private || FDIArgs.PRIV)
  181.       {
  182.         for (i=0; input_string[i] != '('; i++);
  183.         input_string[i]='\0';
  184.         
  185.         fprintf(inc_file,"_LVO%s ",input_string);
  186.  
  187.         tabs = maxtabs - ((i + 5) / 8);
  188.         for (i=0; i < tabs; i++)
  189.           fputs("\t",inc_file);
  190.         
  191.         fputs("EQU\t",inc_file);
  192.         
  193.         if (FDIArgs.DEC)
  194.           fprintf(inc_file,"-%d\n",lvo_bias);
  195.         else
  196.           fprintf(inc_file,"-$%04X\n",lvo_bias);
  197.       }
  198.       lvo_bias += 6;
  199.     }
  200.   }
  201.   
  202.  
  203.   /* Create macros */
  204.   if (!base_def)
  205.     fputs("\tWarning: Can't create CALL macros; no \"##base\" statment.\n",out_file);
  206.   else if (FDIArgs.CALL || FDIArgs.RCALL || FDIArgs.SCALL)
  207.   {
  208.     for(i=0; base_string[i] != '\0'; i++)
  209.       ;
  210.     base_string[i-1] = '\0';
  211.     
  212.     strcpy(macro_string,base_string+1);
  213.     
  214.     for(i=0; macro_string[i] != 0; i++)
  215.       macro_string[i] = toupper(macro_string[i]);
  216.     macro_string[i-4] = '\0';
  217.     
  218.     /* Standard CALL macro */
  219.     if (FDIArgs.CALL)
  220.     {
  221.       fprintf(inc_file,"\nCALL%s\tmacro\n",macro_string);
  222.       fprintf(inc_file,"\tmove.l\t%s,a6\n",base_string);
  223.       fprintf(inc_file,"\tjsr\t_LVO\\1(a6)\n");
  224.       fprintf(inc_file,"\tendm\n");
  225.     }
  226.     
  227.     /* PC Reletive CALL macro */
  228.     if (FDIArgs.RCALL)
  229.     {
  230.       fprintf(inc_file,"\nRCALL%s\tmacro\n",macro_string);
  231.       fprintf(inc_file,"\tmove.l\t%s(pc),a6\n",base_string);
  232.       fprintf(inc_file,"\tjsr\t_LVO\\1(a6)\n");
  233.       fprintf(inc_file,"\tendm\n");
  234.     }
  235.     
  236.     /* Simple CALL macro */
  237.     if (FDIArgs.SCALL)
  238.     {
  239.       fprintf(inc_file,"\nSCALL%s\tmacro\n",macro_string);
  240.       fprintf(inc_file,"\tjsr\t_LVO\\1(a6)\n");
  241.       fprintf(inc_file,"\tendm\n");
  242.     }
  243.   }
  244.   
  245.   fclose(nil_file);
  246.   fclose(fd_file);
  247.   if (FDIArgs.INC_NAME)
  248.     fclose(inc_file);
  249.   FreeArgs(rdargs);
  250. }
  251.  
  252. /* Output program banner */
  253. void OutputBanner(FILE *out_file)
  254. {
  255.   fputs("\033[1m",out_file);  /* Bold text */
  256.   fputs(""PROG_NAME" v"PROG_VERS" © "PROG_DATE" Karl J. Ots\n",out_file);
  257.   fputs("\033[0m",out_file);  /* Plain text */
  258.   fputs("\n",out_file);
  259. }
  260.  
  261. /* Output program usage */
  262. void OutputUsage(void)
  263. {
  264.   OutputBanner(stderr);
  265.   fputs("Bad Args!\n",stderr);
  266.   fputs("Type \"FDI ?\" for template.\n",stderr);
  267.   fputs("\n",stderr);
  268. }
  269.  
  270. /* Disable automatic program abortion if present */
  271. void chkabort(void)
  272. {
  273.   return;
  274. }
  275.